package com.spring.utils;


import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;


/**
 * 校验
 * HibernateValidator工具类
 */

public class HibernateValidatorUtil {

    private static Validator validator = Validation.byProvider(HibernateValidator.class)
            .configure()
            .failFast(true)
            .buildValidatorFactory().getValidator();

    public static <T> boolean validator(T domain) {
        Set<ConstraintViolation<T>> violationSet = validator.validate(domain);
        if (violationSet.size() > 0) {
            System.out.println(violationSet.iterator().next().getMessage() + ",校验失败");
            return false;
        }
        return true;
    }

}
